package it.tukano.treditor.engine.scene;

import it.tukano.check.Checks;
import java.io.Serializable;
import java.util.ArrayList;

/**
 * A node elements contains other scene elements
 * @author pgi
 */
public class NodeElement implements SceneElement, Serializable {

    /* default serial version uid*/
    private static final long serialVersionUID = 0;
    private String name;
    private SceneElement parent;
    private final ArrayList<SceneElement> children = new ArrayList<>();
    
    /**
     * Sets the parent of this node
     * @param newParent the new parent
     */
    public synchronized void setParent(SceneElement newParent) {
        Checks.True(newParent != this);
        this.parent = newParent;
    }
    
    /**
     * Add a child to this node. Set the parent of e to this
     * @param e the new child
     */
    public synchronized void addChild(SceneElement e) {
        Checks.True(e != this);
        children.add(e);
        e.setParent(this);
    }
    
    /** Removes an element from this node */
    public synchronized void removeChild(SceneElement e) {
        Checks.True(e != this);
        children.remove(e);
    }

    /**
     * Returns the name of this element
     * @return the name of this element
     */
    public synchronized String getName() {
        return name;
    }
    
    /**
     * Sets the name of this element
     * @param name the new name of this element
     */
    public synchronized void setName(String name) {
        this.name = name;
    }

    /**
     * Returns the parent of this element
     * @return the parent of this element
     */
    public synchronized SceneElement getParent() {
        return parent;
    }
 
    /**
     * Returns the amount of children in this node
     * @return how many child elements are added to this node
     */
    public synchronized int getChildrenCount() {
        return children.size();
    }
    
    /**
     * Returns the child at the given index
     * @param index the index of the requested child
     * @return the child at index
     */
    public synchronized SceneElement getChild(int index) {
        return children.get(index);
    }

    /**
     * Returns the type of this SceneElement
     * @return the type of this scene element
     */
    public Type getType() {
        return Type.NODE;
    }

    @Override
    public String toString() {
        return name;
    }
}
